package cn.lx.es.query.processor;

import cn.lx.es.query.annotation.MultiMatchSearch;
import cn.lx.es.query.annotation.SearchProcessor;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.lang.reflect.Field;

/**
 * 内置处理器 - 多字段匹配处理器
 */
@SearchProcessor
public class SearchMultiMatchProcessor implements SearchAnnotationProcessor<MultiMatchSearch> {
    @Override
    public void process(MultiMatchSearch annotation, Field field, Object value, BoolQueryBuilder boolQueryBuilder) {
        if (value != null) {
            String[] fields = annotation.fields().length == 0 ? new String[]{field.getName()} : annotation.fields();
            boolQueryBuilder.must(QueryBuilders.multiMatchQuery(value, fields));
        }
    }
    @Override
    public Class<MultiMatchSearch> getAnnotationType() {
        return MultiMatchSearch.class;
    }
}
